<?php
	/** 
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Administrative 
	* @author Ray Nowell 
	*/  
class Drug extends MY_Controller {

	function Drug()
	{
		parent::MY_Controller(MUST_BE_ADMIN);
	}
	
	//Private Functions:
	function _get_form_post_data($add_record = false)
	{
		$this->load->helper('date');

		$data = array(
						'drug_id' 				=> $this->input->post('drug_id'),
						'item_desc'				=>	$this->input->post('item_desc'),
						'generic_desc'			=>	$this->input->post('generic_desc'),
						'size_code'				=>	$this->input->post('size_code'),
						'size_qty'				=>	$this->input->post('size_qty'),
						'strength_code'			=>	$this->input->post('strength_code'),
						'strength_qty'			=>	$this->input->post('strength_qty'),
						'nddf_awp'				=>	$this->input->post('nddf_awp'),
						'nddf_ndc_number'		=>	$this->input->post('nddf_ndc_number'),
						'nddf_package_size_qty'	=>	$this->input->post('nddf_package_size_qty'),
						'pack_size'				=>	$this->input->post('pack_size'),
						'wholesale_cost'		=>	$this->input->post('wholesale_cost'),
						'wholesale_each_cost'	=>	$this->input->post('wholesale_each_cost'),
						'doses_per_day'			=>	$this->input->post('doses_per_day'),		
						'modified_date'			=>	date("Y-m-d H:i:s"),
						'modified_by'			=>	$this->ion_auth->get_user()->username,
		);
    
    if ($add_record)
      array_shift($data);
		
		return $data;
	}
	
	function _get_additional_javascript()
	{
		$ret_val = '$("#_itemDesc").focus();';
		return $ret_val; 
	}
	
	function add()
	{
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Drug_model');

		$data['title'] = COMPANY_NAME." -- Add Drug ";
		$data['heading'] = "Drug Form";
	
		//Set the values for the Drug View
		$data['drug_id'] 				=	'';
		$data['item_desc'] 				=	'';
		$data['generic_desc']			=	'';
		$data['size_code']				=	'';
		$data['size_qty']				=	'';
		$data['strength_code']			=	'';
		$data['strength_qty']			=	'';
		$data['nddf_awp']				=	'';
		$data['nddf_ndc_number']		=	'';
		$data['nddf_package_size_qty']	=	'';
		$data['pack_size']				=	'';
		$data['wholesale_cost']			=	'';
		$data['wholesale_each_cost']	=	'';
		$data['doses_per_day']			=	'';
		$data['created_date']			=	'';
		$data['modified_date']			=	'';
		$data['created_date'] 			=	'';
		$data['modified_date'] 			=	'';

		$data['save_type'] = 'save_new';
		$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript();
						
		$this->load->view('drug_view', $data);	 
		
	}
	 
	function edit()
	{
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->model('Drug_model');
			
		//$this->output->enable_profiler(TRUE); 

		$data['title'] = COMPANY_NAME." -- Drug Edit";
		$data['heading'] = "Drug Edit";
		
		//The number of segments will determine the edit mode of the intervention:	single-list or multi-record navigation
		$total_segments = $this->uri->total_segments();
		
		if ($total_segments == SINGLE_EDIT)
		{			
			$drug_id = $this->uri->segment(3, 0);	//Three is the Intervention Id.	zero is the default value
			$data['additional_javascript_doc_ready_functions'] = $this->_get_additional_javascript().get_save_javascript('drug');
		}
		else 
		{
			$segment_to_get = $this->uri->segment(3);

			$drug_id = $this->uri->segment($segment_to_get);
			
			$previous_segment = $segment_to_get <= 4 ? 4 : $segment_to_get - 1;
			$next_segment = $segment_to_get == $total_segments	? $total_segments : $segment_to_get + 1;
			$fullUri = $this->uri->uri_string();
			$search_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3);
			$replace_text = $this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'|';
			$current_uri = str_replace($search_text, $replace_text, $fullUri);
			$nav_url = base_url().'index.php'.$current_uri;
			$data['additional_javascript_doc_ready_functions'] = get_navigation_doc_ready_javascript().$this->_get_additional_javascript().get_save_javascript('drug');
			$data['additional_jquery_javascript'] = get_navigation_functions($nav_url, $previous_segment, $next_segment, $total_segments);
			$data['nav_bar'] = get_navigation_markup(base_url(), $segment_to_get - 3, $total_segments - 3);
			
		}

		$drug_row = $this->Drug_model->get_drug_id_record($drug_id);
		
		//Set the values for the Drug View
		$data['drug_id']				=	$drug_row->drug_id;
		$data['item_desc']				=	$drug_row->item_desc;
		$data['generic_desc']			=	$drug_row->generic_desc;
		$data['size_code']				=	$drug_row->size_code;
		$data['size_qty']				=	$drug_row->size_qty;
		$data['strength_code']			=	$drug_row->strength_code;
		$data['strength_qty']			=	$drug_row->strength_qty;
		$data['nddf_awp']				=	$drug_row->nddf_awp;
		$data['nddf_ndc_number']		=	$drug_row->nddf_ndc_number;
		$data['nddf_package_size_qty']	=	$drug_row->nddf_package_size_qty;
		$data['pack_size']				=	$drug_row->pack_size;
		$data['wholesale_cost']			=	$drug_row->wholesale_cost;
		$data['wholesale_each_cost']	=	$drug_row->wholesale_each_cost;
		$data['doses_per_day']			=	$drug_row->doses_per_day;
		$data['created_date']			=	$drug_row->created_date;
		$data['modified_date']			=	$drug_row->modified_date;

		$data['save_type'] = 'update';
		
		$this->load->view('drug_view', $data);	 
		
	}
	
	/** 
	 * Calls the Drug Model to save the patient based on the post data passed
	 * @access public */
	function save_new()
	{
		$data = $this->_get_form_post_data(true);
		
		$data['created_date'] = date('Y-m-d H:i:s');
		$data['created_by'] = $this->ion_auth->get_user()->username;
		
		$this->Drug_model->Add_drug($data);
		redirect('drug/add');
	}
	
	/** 
	 * Calls the Drug Model to update the drug based on the post data passed
	 * @access public */
	function update()
	{
		$data = $this->_get_form_post_data();
		$this->Drug_model->Update_drug($data);
		redirect('drug/edit/'.element('drug_id', $data));
	}

}

/* End of file drug.php */
/* Location: ./system/application/controllers/drug.php */
